package com.bd.msdp.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;

import com.bd.msdp.auth.oauth.MsdpUserApprovalHandler;

/**
 * @author Rob Winch
 * 
 */
@Configuration
public class OAuth2ServerConfig {

	protected static class Stuff {
		
		@Autowired
		private ClientDetailsService clientDetailsService;
	
		@Autowired
		private TokenStore tokenStore;
	
		@Bean
		public ApprovalStore approvalStore() throws Exception {
			TokenApprovalStore store = new TokenApprovalStore();
			store.setTokenStore(tokenStore);
			return store;
		}
	
		@Bean
		@Lazy
		@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS)
		public MsdpUserApprovalHandler userApprovalHandler() throws Exception {
			MsdpUserApprovalHandler handler = new MsdpUserApprovalHandler();
			
			handler.setApprovalStore(approvalStore());
			handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
			handler.setClientDetailsService(clientDetailsService);
			handler.setUseApprovalStore(true);
			
			return handler;
		}
	}
	
}
